import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * Created by aleks_000 on 15.12.13.
 */
public class Program {
    private static final String URL = "jdbc:sqlite:test.db";
    private static final String DB_USERNAME = "test";
    private static final String DB_PASSWORD = "test";
    private static final String STUDENT_FORMAT = "Id: %d; Name: %s; Group: %s; Mark: %f";
    private static final BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            return;
        }
        SQLiteDataSource sqLiteDataSource = new SQLiteDataSource();
        sqLiteDataSource.setUrl(URL);

        while (true) {
            System.out.println("get-all: retrieve all students");
            System.out.println("get: get student by id");
            System.out.println("create: create a new student");
            System.out.println("update: update the student");
            System.out.println("delete: delete a student");
            System.out.println("exit: exit from app");
            String cmd = null;
            try {
                cmd = consoleReader.readLine();
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                System.out.println("Something wrong with command. Try again");
                continue;
            }
            if (cmd.equalsIgnoreCase("get-all")) {
                getAllStudents(sqLiteDataSource);
                System.out.println();
                continue;
            }
            if (cmd.equalsIgnoreCase("get")) {
                getStudent(sqLiteDataSource);
                System.out.println();
                continue;
            }
            if (cmd.equalsIgnoreCase("create")) {
                createStudent(sqLiteDataSource);
                System.out.println();
                continue;
            }
            if (cmd.equalsIgnoreCase("update")) {
                updateStudent(sqLiteDataSource);
                System.out.println();
                continue;
            }
            if (cmd.equalsIgnoreCase("delete")) {
                deleteUser(sqLiteDataSource);
                System.out.println();
                continue;
            }
            if (cmd.equalsIgnoreCase("exit")) {
                break;
            }
            System.out.println("Incorrect command. Try again");
        }

        try {
            consoleReader.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void getAllStudents(DataSource dataSource) {
        try {
            Connection connection = dataSource.getConnection(DB_USERNAME, DB_PASSWORD);
            StudentRepository repository = new StudentRepository(connection);
            Collection<Student> students = repository.getAll();
            for(Student student : students) {
                System.out.println(String.format(STUDENT_FORMAT,
                        student.getId(), student.getName(), student.getGroupNumber(), student.getMark()));
            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void getStudent(DataSource dataSource) {
        System.out.print("Enter id of the student: ");
        int id = 0;
        try {
            id = Integer.parseInt(consoleReader.readLine());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Incorrect id");
        }
        try {
            Connection connection = dataSource.getConnection();
            StudentRepository repository = new StudentRepository(connection);
            Student student = repository.get(id);
            connection.close();
            if (student == null) {
                System.out.println("Student not found!");
            } else {
                System.out.println("Found student:");
                System.out.println(String.format(STUDENT_FORMAT,
                        student.getId(), student.getName(), student.getGroupNumber(), student.getMark()));
            }
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    private static void createStudent(DataSource dataSource) {
        try {
            Student student = new Student();
            System.out.print("Student name: ");
            student.setName(consoleReader.readLine());
            System.out.print("Group number: ");
            student.setGroupNumber(consoleReader.readLine());
            System.out.print("Mark: ");
            student.setMark(Double.parseDouble(consoleReader.readLine()));
            Connection connection = dataSource.getConnection();
            StudentRepository repository = new StudentRepository(connection);
            Student addedStudent = repository.create(student);
            connection.close();
            System.out.println("Added student:");
            System.out.println(String.format(STUDENT_FORMAT,
                    addedStudent.getId(), addedStudent.getName(), addedStudent.getGroupNumber(), addedStudent.getMark()));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void updateStudent(DataSource dataSource) {
        try {
            Student student = new Student();
            System.out.print("Student id: ");
            student.setId(Integer.parseInt(consoleReader.readLine()));
            System.out.print("Student name: ");
            student.setName(consoleReader.readLine());
            System.out.print("Group number: ");
            student.setGroupNumber(consoleReader.readLine());
            System.out.print("Mark: ");
            student.setMark(Double.parseDouble(consoleReader.readLine()));
            Connection connection = dataSource.getConnection();
            StudentRepository repository = new StudentRepository(connection);
            boolean result = repository.update(student);
            connection.close();
            if (result) {
                System.out.println("Student updated.");
            } else {
                System.out.println("Student is not update. Maybe he does not exist in db");
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    private static void deleteUser(DataSource dataSource) {
        try {
            System.out.print("Student id: ");
            int id = Integer.parseInt(consoleReader.readLine());
            Connection connection = dataSource.getConnection();
            StudentRepository repository = new StudentRepository(connection);
            boolean result = repository.delete(id);
            connection.close();
            if (result) {
                System.out.println(String.format("Student with id %d deleted", id));
            } else {
                System.out.println(String.format("Can't delete student with id %d. Maybe, he is not exist in db"));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
